package com.lgj.feign;

import com.lgj.feign.GrayscaleServiceInstanceLoadBalancer;
import com.lgj.feign.GrayscaleWhiteListUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: guangjie.liao
 * @Date: 2023/8/16 19:23
 * @Description: 灰度发布负载均衡策略配置类
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
public class GrayscaleConfiguration {

    @Value("${spring.application.name}")
    private String serverName;

    @Autowired
    private GrayscaleWhiteListUser grayWhiteListUser;

    @Bean
    @ConditionalOnMissingBean
    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(LoadBalancerClientFactory loadBalancerClientFactory) {
        return new GrayscaleServiceInstanceLoadBalancer(loadBalancerClientFactory.getLazyProvider(serverName, ServiceInstanceListSupplier.class),grayWhiteListUser);
    }
}
